import { BehaviorHeartBeat } from "./ai/BehaviorHeartBeat";
import { BehaviorLogin } from "./ai/BehaviorLogin";
import { BehaviorVote } from "./ai/BehaviorVote";
import { IBehavior } from "./behavior/Behavior";
import { BehaviorTree } from "./behavior/BehaviorTree";
import { RobotConnection } from "./RobotConnection";
import { GameRoomInfo } from "../../../../common/model/GameRoomInfo";
import { UserGameData } from "../../../../common/model/UserGameData";
import { IMsg } from "../../../../framework/service/Components/WorkOrderInfo";
import { Tools } from "../../../../framework/utils/Tools";
import _ from "lodash";
import { EnumProtocolType } from "../../../../common/protocols/EnumProtocolType";
import { SC_GameRoomChanged } from "../../../../common/protocols/Proto_GameRoomChanged";
import { IGameRoom } from "../logic/room/GameRoomEntity";

export class RobotEntity {

    public uid: string = Tools.getUID();



    public username: string = "";

    public password: string = "";

    public _data: UserGameData;

    private _connection: RobotConnection = new RobotConnection();

    private _aiTree: BehaviorTree;

    private _gameRoomList: GameRoomInfo[] = [];




    public _userId: string = "";


    private _isRunning: boolean = false;

    public getConnection(): any {

        return this._connection;
    }

    constructor() {

        this.init();
    }
    private init(): void {
        this._aiTree = new BehaviorTree(this);


        const behaviors: IBehavior[] = [
            new BehaviorLogin(),
            new BehaviorHeartBeat(),
            new BehaviorVote()
        ];
        _.each(behaviors, b => {
            b.bindEntity(this);
            this._aiTree.addBehavior(b);
        });
    }


    public sendMsg(msg: IMsg, callback: (msg: IMsg) => void): void {
        this._connection.sendMsg(msg, callback);

    }

    public setData(data: UserGameData, userid: string): void {
        this._data = data;
        this._userId = userid;
    }


    public setRoomList(list: GameRoomInfo[]): void {
        this._gameRoomList = list;
    }

    public getRoomList(): GameRoomInfo[] {
        return this._gameRoomList;
    }


    public isLogined(): boolean {
        return this._data != null;
    }

    public start(): void {
        this._isRunning = true;
    }

    public stop(): void {
        this._isRunning = false;
    }

    public tick(): void {
        if (!this._isRunning) {
            return;
        }


        this._aiTree.tick();
    }

    public setCurRoom(room: GameRoomInfo): void {

        if (_.find(this._gameRoomList, { uid: room.uid })) {
            this._gameRoomList.push(room);
        }
        this._data.roomId = room.uid;
    }

    public onReceiveMsg(msg: IMsg): void {
        switch (msg.cmdid) {
            case EnumProtocolType.SC_GameRoomChanged: {
                const sc: SC_GameRoomChanged = msg as SC_GameRoomChanged;

                const room: GameRoomInfo = _.find(this._gameRoomList, { uid: sc.room.uid });
                    
                _.each(Object.keys(sc.room),key=>{
                    room[key] = sc.room[key];
                });
                
                break;
            }
            default: {
                break;
            }
        }
    }
}